package com.hjj.concurrence.syncMethod.t2;

/**
 * 如果多个线程共同访问一个对象中的实例变量，则有可能出现非线程安全的问题
 *
 * 两个线程同时访问同一个对象中的同步方法时一定是线程安全的。
 */
public class HasSelfPrivateNum {
    private int num;

    public /*synchronized*/ void addI(String username) {
        try {
            if (username.equals("a")) {
                num = 100;
                System.out.println("a set over!");
                Thread.sleep(2000);
            } else {
                num = 200;
                System.out.println("b set over!");
            }
            System.out.println(username + " num: " + num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HasSelfPrivateNum num = new HasSelfPrivateNum();
        new ThreadA(num).start();
        new ThreadB(num).start();
    }
}
